package org.kim.utils;

import org.kim.core.Logger;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class CommandUtils {

    public static void printHelpInfo() {
        Logger.info(Constants.helpInfo);
    }

    public static boolean commandValidate(String command) {
        String regx = "(register|login|add|list|search|borrow|delete|return|exit)";
        Pattern pattern = Pattern.compile(regx);
        Matcher matcher = pattern.matcher(command);
        if (matcher.find()) {
            return true;
        }

        return false;
    }

    public static String[] parseInput(String command) {
        String[] strs = new String[0];
        Set<String> bookCommandSet = new HashSet<>(Arrays.asList("add", "search", "borrow", "delete", "return"));
        int spaceIndex = command.indexOf(" ");
        if (spaceIndex > 0 && bookCommandSet.contains(command.substring(0, spaceIndex))) {
            strs = command.split("\"");
            List<String> list = new ArrayList<String>(Arrays.asList(strs));
            list.remove(2);
            strs = list.stream().map(e -> e.trim()).toArray(String[]::new);
        } else {
            strs = command.split(" ");
        }
        return strs;
    }

    public static void main(String[] args) {
        parseInput("search \"book 111\" \"tom son\" 1");
    }
}
